import java.util.PriorityQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2025-01-24
 * Time: 11:46
 */
class MedianFinder {
    private PriorityQueue<Integer> left;//左边是大根堆，右边是小根堆
    private PriorityQueue<Integer> right;


    public MedianFinder() {
        left = new PriorityQueue<>((a,b) -> b-a);
        right = new PriorityQueue<>((a,b) -> a-b);
    }

    public void addNum(int num) {
        if(left.size() == right.size()){
            if(left.isEmpty() || num <= left.peek()){
                left.offer(num);
            }else if(num > left.peek()){
                right.offer(num);
                left.offer(right.poll());
            }
        }else if(left.size() - right.size() == 1){
            if(num <= left.peek()){
                left.offer(num);
                right.offer(left.poll());
            }else if(num > left.peek()){
                right.offer(num);
            }
        }
    }

    public double findMedian() {
        if(left.size() == right.size()){
            double ret = ( left.peek() + right.peek() ) / 2.0;
            return ret;
        }else{
            return left.peek();
        }
    }
}

/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder obj = new MedianFinder();
 * obj.addNum(num);
 * double param_2 = obj.findMedian();
 */
public class Test4 {
}
